import lodash from 'lodash';
import * as data from './data';
import * as institutionCore from '../../sys/institution/core';
import log from '../../../log';
import * as util from '../../../util/index';

const likeParms = 'name'.split(',');
export function fetch(query, page, pageSize, user) {
    return util.resolve().then(() => {
        log.info('fetch activity params----', query);
        log.info('fetch activity user----', user);
        const pageOpt = {
            page,
            pageSize
        };
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        //业务员只能获取本机构和上级所有层级机构的活动列表
        if (user.userType === 2) {
            return institutionCore.getAllLevelPids(user.institutionId).then(r => {
                where.push(['institutionId', 'in', r]);
                return data.fetch(where, 1, pageOpt, user).then(res => {
                    log.info('fetch activity result----', res.toJSON());
                    return util.data(res, 1);
                });
            });
        }
        //机构管理员只能看本机构和所有子机构的活动列表
        if (user.userType === 3) {
            return institutionCore.getAllLevelSonIds(user.institutionId).then(r => {
                where.push(['institutionId', 'in', r]);
                return data.fetch(where, 1, pageOpt, user).then(res => {
                    log.info('fetch activity result----', res.toJSON());
                    return util.data(res, 1);
                });
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.fetch(where, 1, pageOpt, user).then(res => {
            log.info('fetch activity result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch activity is err----', err);
        return err;
    });
}
export function find(query, user) {
    return util.resolve().then(() => {
        log.info('find activity params----', query);
        log.info('find activity user----', user);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        //业务员只能获取本机构和上级所有层级机构的活动列表
        if (user.userType === 2) {
            return institutionCore.getAllLevelPids(user.institutionId).then(r => {
                where.push(['institutionId', 'in', r]);
                return data.find(where, 1, user).then(res => {
                    log.info('find activity result----', util.data(res));
                    return util.data(res);
                });
            });
        }
        //机构管理员只能看本机构和所有子机构的活动列表
        if (user.userType === 3) {
            return institutionCore.getAllLevelSonIds(user.institutionId).then(r => {
                where.push(['institutionId', 'in', r]);
                return data.find(where, 1, user).then(res => {
                    log.info('find activity result----', util.data(res));
                    return util.data(res);
                });
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1, user).then(res => {
            log.info('find activity result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find activity is err----', err);
        return err;
    });
}
export function add(activity) {
    return util.resolve().then(() => {
        log.info('add activity params----', activity);
        return data.find([{name: activity.name}], 0).then(res => {
            if (res.toJSON().length > 0) {
                throw '活动名称重复！';
            }
            activity.id = util.uuid();
            activity.status = '1';
            //活动图片
            const activityImages = [];
            const images = lodash.cloneDeep(activity).images || [];
            images.forEach(i => {
                activityImages.push({
                    id: util.uuid(),
                    activityId: activity.id,
                    url: i.url,
                    displayOrder: i.displayOrder || 0
                });
            });
            delete activity.images;
            return data.add(activity, activityImages).then(() => {
                activity.images = activityImages;
                log.info('add activity result----', JSON.stringify(activity));
                return activity;
            });
        });
    }).catch(err => {
        log.error('add activity is err----', err);
        return err;
    });
}
export function edit(activity) {
    return util.resolve().then(() => {
        log.info('edit activity params----', activity);
        return data.find([['id', '<>', `${activity.id}`], ['name', '=', `${activity.name}`]], 1).then(res => {
            if (res.toJSON().length > 0) {
                throw '活动名称重复！';
            }
            //活动图片
            const activityImages = [];
            const images = lodash.cloneDeep(activity).images || [];
            images.forEach(i => {
                activityImages.push({
                    id: util.uuid(),
                    activityId: activity.id,
                    url: i.url,
                    displayOrder: i.displayOrder || 0
                });
            });
            delete activity.images;
            return data.edit(activity, activityImages).then(() => {
                activity.images = activityImages;
                log.info('edit activity result----', JSON.stringify(activity));
                return activity;
            });
        });
    }).catch(err => {
        log.error('edit activity is err----', err);
        return err;
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete activity params----', id);
        return data.del(id).then(res => {
            log.info('delete activity result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete activity is err----', err);
        return err;
    });
}
